package com.itqdkj.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itqdkj.health.entity.PageResult;
import com.itqdkj.health.entity.QueryPageBean;
import com.itqdkj.health.entity.Result;
import com.itqdkj.health.pojo.Menu;
import com.itqdkj.health.pojo.Role;
import com.itqdkj.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 角色控制
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 角色创建
     * @param
     * @return
     */
    @RequestMapping("/add")
    public Result roleadd(@RequestBody Role role,Integer[] menus){
        try {
            roleService.roleadd(role,menus);
            return new Result(true,"角色创建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }

    /**
     * 角色信息修改
     * @param map
     * @return
     */
    @RequestMapping("/update")
    public Result roleUpdate(Map map){
        try {
            roleService.roleUpdate(map);
            return new Result(true,"角色信息修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色信息修改失败");
        }
    }

    /**
     * 角色删除
     * @param role_id
     * @return
     */
    @RequestMapping("/delete")
    public Result roleDelete(@RequestParam("id") Integer role_id){
        try {
            roleService.roleDelete(role_id);
            return new Result(true,"删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }

    /**
     * 角色权限添加
     * @param role_id 角色id
     * @param menuidList 权限id
     * @return
     */
    @RequestMapping("/menusadd")
    public Result menusAdd(@RequestParam("id") Integer role_id, List<Integer> menuidList){
        try {
            roleService.menusAdd(role_id,menuidList);
            return new Result(true,"角色权限添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色权限添加失败");
        }
    }

    /**
     * 角色权限删除
     * @param role_id
     * @param menuidList
     * @return
     */
    @RequestMapping("/memudelete")
    public Result memuDelete(@RequestParam("id") Integer role_id, List<Integer> menuidList){
        try {
            roleService.memuDelete(role_id,menuidList);
            return new Result(true,"角色权限删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"角色权限删除失败");
        }
    }

    /**
     * 查询所有权限
     * @return
     */
    @RequestMapping("/findmenus")
    public Result findMenus(){
        try {
            List<Menu> menuList = roleService.findMenus();
            return new Result(true,"权限查询成功",menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限查询失败");
        }
    }

    @RequestMapping("/findPageAll")
    public PageResult findPageAll(@RequestBody QueryPageBean queryPageBean){
        return roleService.findPageAll(queryPageBean);
    }
}
